package advanced.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex {

public static Pattern pattern;
public static Matcher matcher;

/**
 * Prints the text that matches the given pattern
 * @param input array of string
 * @param regEx regular expression pattern
 * @param notes a description of the pattern
 */
public static void find(String input[], String regEx, String notes) {

    Pattern pattern = Pattern.compile(regEx);

    System.out.println(notes + ":");

    for (int i = 0; i < input.length; i++) {
            matcher = pattern.matcher(input[i]);
            if (matcher.find()) {
                    System.out.println(input[i]);
            }
    }
    System.out.println();

}

public static void main(String[] args) throws Exception {

    String[] lines = { 
                    "Good day!", 
                    "http://google.com/",
                    "sandy123@gmail.com", 
                    "09.13.1900", 
                    "Java regex example",
                    "223-99-3312", 
                    "03/23/1982",
                    "Jeremy is very happy today because the weather is nice!",
                    "http://www.yahoo.com", 
                    "http://www.albany.edu", 
                    "341-92-2341",
                    "800-234-1023", 
                    "ken908@yahoo.com", 
                    "03-23-1999",
                    "http://www.health.org",
                    "123456784589"
                    };

    // Regular expression patterns
    String emailRegex = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
    String websiteRegex = "https?://(www\\.)?[A-Za-z0-9]+\\.(com|org|edu|gov|us)/?.*";
    String ssnRegex = "^[0-9]{3}-[0-9]{2}-[0-9]{4}$";
    String dateRegex = "^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.][0-9]{4}$";
    String startWithGoodRegex = "^(Good).*";
    String endWithExampleRegex =".*(example)$";
    String happyRegex = ".*happy.*";
    String number="(\\d)(?=(\\d{3})+$)";
    // Finding text with each pattern
    find(lines, emailRegex, "Emails");
    find(lines, websiteRegex, "Websites");
    find(lines, ssnRegex, "Social Security Numbers");
    find(lines, dateRegex, "Dates");
    find(lines, startWithGoodRegex, "Sentence starts with 'Good'");
    find(lines, happyRegex, "Sentence contains 'happy'");
    find(lines, endWithExampleRegex, "Sentence ends with 'example'");
    find(lines, number, "numbers");    
}

}
